微服务学习2——利用nacos实现服务治理

(本文参考黑马程序员项目)
个人仓库地址:https://gitee.com/jkangle/springboot-exercise.git

1.当前项目中存在的问题

不同微服务之间的调用通过硬编码的形式,这样可能在需要打量修改的时候出现问题,因此引入了服务治理的概念。服务治理可以实现微服务的自动化注册与发现

nacos是一个常见的服务注册中心

2.nacos的搭建

  • 下载安装包zip
  • 解压进入到bin
  • 执行startup.cmd -m standalone
  • 启动成功
  • 打开浏览器输入http://localhost:8848/nacos,即可访问服务, 默认密码是nacos/nacos

为了方便启动可以将启动写成一个bat文件,下次启动的时候只需要双击启动文件即可

D:
cd D:\environments\nacos\nacos\bin
startup.cmd -m standalone

微服务学习2——利用nacos实现服务治理_第1张图片

3.将各个模块放入到nacos中

3.1在父模块中添加需要的版本信息(一定要注意版本号)

版本号文档
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>testartifactId>
    <version>1.0-SNAPSHOTversion>


    <packaging>pompackaging>
    <modules>
        <module>shop-commonmodule>
        <module>shop-usersmodule>
        <module>shop-productmodule>
        <module>shop-ordermodule>
    modules>

    <properties>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>

        <spring-boot.version>2.6.13spring-boot.version>
        <spring-cloud.version>2021.0.5spring-cloud.version>
        <spring-cloud-alibaba.version>2021.0.5.0spring-cloud-alibaba.version>
        <fastjson.version>1.2.57fastjson.version>
    properties>

    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${spring-boot.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring-cloud-alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>${fastjson.version}version>
            dependency>

        dependencies>
    dependencyManagement>

project>
3.2将商品模块放到nacos中

添加依赖,修改配置文件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.examplegroupId>
        <artifactId>testartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>shop-productartifactId>

    <properties>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.examplegroupId>
            <artifactId>shop-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
    dependencies>

project>

配置文件

server:
  port: 8081

spring:
  application:
    name: service-product
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8
    username: root
    password:
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect


3.3将订单模块放到nacos中

订单模块同理


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.examplegroupId>
        <artifactId>testartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>shop-orderartifactId>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.examplegroupId>
            <artifactId>shop-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-nacos-discoveryartifactId>
            <version>2.1.0.RELEASEversion>
        dependency>

    dependencies>

project>
server:
  port: 8091

spring:
  application:
    name: service-order
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8
    username: root
    password:

  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

4.现在利用nacos进行调用

修改controller代码即可

package org.example.controller;

import com.alibaba.fastjson.JSON;
import org.example.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import pojo.Order;
import pojo.Product;

/**
 * 使用restTemplate对象调用shop-product的请求方法
 */
@RestController
public class OrderController {
    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid){
        ServiceInstance serviceInstance = discoveryClient.getInstances("service-product").get(0);
        String url = serviceInstance.getHost() + ":" +serviceInstance.getPort();
        Product product = restTemplate.getForObject("http://" +url + "/product/"+ pid, Product.class);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setOid(2);
        order.setUid(2);
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(2);
//        orderService.save(order);
        return order;
    }

}

在这里插入图片描述

你可能感兴趣的:(Java学习笔记,微服务,学习,架构)