Spring Cloud 微服务实战

服务治理:Spring Cloud Euraka

  • 第一步,搭建服务注册中心(如果用zk作为注册中心,类似安装配置启动zk)
    注册中心,我们部署两套,避免单点(小集群)
    
    /**
     * @EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话
     */
    @SpringBootApplication
    @EnableEurekaServer
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
            System.out.println("Spring Cloud Eureka Server1 start success!");
        }
    }
    注册中心1的application.yml核心配置:
    spring:
      application:
        name: eureka-server
    server:
      port: 8001
    eureka:
      client:
        service-url:
          defaultZone: http://172.16.1.30:8001/eureka  定义注册中心的地址,服务注册需要制定
    
    
    @SpringBootApplication
    @EnableEurekaServer
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
            System.out.println("Spring Cloud Eureka Server2 start success!");
        }
    }
    注册中心2的application.yml核心配置:
    spring:
      application:
        name: eureka-server
    server:
      port: 8001
    eureka:
      client:
        service-url:
          defaultZone: http://172.16.1.31:8001/eureka  定义注册中心的地址,服务注册需要制定
    
    
    注册中心启动很简单,重点看下部署了两台机器,即部署了两套注册中心,集群部署,URL分别是:
    http://172.16.1.30:8001/eureka
    http://172.16.1.31:8001/eureka

     
  • 第二步,注册服务提供者
    假设服务已经写好了,服务需要启动:
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    @EnableHystrix
    @EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
    public class ServiceApplication {
        public static void main(String[] args){
            SpringApplication.run(ServiceApplication.class, args);
            System.out.println("Server start success");
        }
    }
    application.yml关注配置:
    spring:
      application:
        name: eureka-provider  #服务名称
    eureka:
      client:
        service-url:
          defaultZone: http://172.16.1.30:8001/eureka/,http://172.16.1.31:8001/eureka/ 
    指定了服务注册中心的地址(服务往哪里注册,指定的即为上面注册中心的两个地址)
    
    
    定义的服务:
    为了方便先创建一个服务提供者和消费者共同使用的接口模块项目eureka_api,并创建如下接口和请求返回参数实体类:
    public interface UserInterface {
        @PostMapping("/users")
        String getUserName(String name);
    
        @GetMapping("/msg")
        String getUserMsg(String name);
    }
    
    //定义的服务实现
    @RestController
    public class UserController implements UserInterface {
    
        @Autowired
        private HttpServletRequest request;
    
        @Override
        public String getUserName(String name) {
            return "zhengchao"
        }
    
        @Override
        public String getUserMsg(String name) {
            return "boy,29,married";
        }
    }
    这里需要注意的是Controller的两个服务接口中没有再加PostMapping或GetMapping,因为这个由被实现接口申明了
    

     
  • 第三步,服务发现
    启动类:
    @SpringBootApplication
    @EnableDiscoveryClient //消费者客户端
    @EnableFeignClients //feign客户端
    public class EurekaConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaConsumerApplication.class, args);
        }
    }
    使用服务:
    这里采用fegin伪客户端的方式来访问我们服务提供端
    @FeignClient(value = "eureka-provider")
    public interface UserService extends UserInterface {
    }
    通过FeignClient来指定调用的服务端应用名称eureka-provider,这名称对应注册在服务中心的Application目录下 ,在Controller层创建一个响应的输出UserController并分别提供两个展示的接口,代码如:
    @RestController
    public class UserController{
    
        @Autowired
        private UserService userService;
    
        @GetMapping("/users")
        public String getUserName(String name) {
            return userService.getUserName(name);
        }
    
        @GetMapping("/msg")
        public String getUserMsg() {
            return userService.getUserMsg();
        }
    }
    
    application.yml配置:
    eureka:
        client:
          service-url:
            defaultZone: http://172.16.1.30:8001/eureka/,http://172.16.1.31:8001/eureka/ 

以上是简单的服务注册与服务发现的示例,本文核心是分析SpringCloud作为微服务的原理,包括但不限于说明它的:注册中心、服务注册、服务发现、负载均衡、熔断降级等核心知识~
TODO.....


 

你可能想知道的一些知识点:

  1. 对于Spring Boot应用,建议启动程序的包名层次最高,这样Spring Boot默认自动搜索启动程序之下的所有类
  2. 更换Spring Boot默认的启动端口8080,只需要调整application.yml中的server.port=9090即可
  3. Spring Boot不需要配置服务器,因为其内置了比如tomcat
  4. 日志级别设置:
    配置设置:
    logging.level.root=info
    指定某个包下的日志级别:
    logging.level.org=warn
    logging.level.com.aliyun.oss=error
发布了142 篇原创文章 · 获赞 345 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/zhengchao1991/article/details/100752707