# spring-cloud-poc **Repository Path**: holynyll/spring-cloud-poc ## Basic Information - **Project Name**: spring-cloud-poc - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2017-10-16 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # spring-cloud-poc # Eureka Server注册中心 执行如下命令,启动两个Eureka Server: ``` docker-compose up -d eureka-1 eureka-2 ``` 监控后台日志: ``` docker-compose logs -f eureka-1 eureka-2 ``` 启动后,通过浏览器浏览: ``` http://localhost:8761 http://localhost:8762 ``` ![](leanote://file/getImage?fileId=59fac00cb71f06074d000000) ![](leanote://file/getImage?fileId=59fac021b71f06074d000001) 查看配置文件,互为备份的设置: ``` # eureka-1 eureka: instance: hostname: eureka-1 client: serviceUrl: defaultZone: http://eureka-2:8761/eureka/ ``` ``` # eureka-2 eureka: instance: hostname: eureka-2 client: serviceUrl: defaultZone: http://eureka-1:8761/eureka/ ``` # 启动Config Server 执行命令: ``` docker-compose up -d config-server-1 ``` 查看服务注册信息页面 讲解config server原理 # 启动服务Service-hi 启动mysql数据库: ``` docker-compose up -d mysql-1 ``` 启动服务service-hi实例eclient-1和eclient-2执行命令: ``` docker-compose up -d eclient-1 eclient-2 ``` 查看服务注册信息页面 讲解Service-hi服务的实现,如何实现数据库链接,如何开发数据库。 # 启动服务消费者Feign客户端 执行启动命令 ``` docker-compose up -d feign-1 ``` 查看服务注册页面 讲解代码和配置,演示通过服务消费者调用服务。 # 演示Feign Client负载均衡和容错 在浏览器中调用hi接口: ``` http://localhost:8080/hi?name=lalla ``` 关闭eclient-1实例 ``` docker-compose stop eclient-1 ``` 访问: ``` http://localhost:8080/hi?name=lalla ``` # 演示熔断 在关掉eclient-1后,继续关闭eclient-2: ``` docker-compose stop eclient-2 ``` 访问: ``` http://localhost:8080/hi?name=lalla ``` 讲解代码层面如何实现熔断 # 演示1个Eureka Server注册中心 停止一个Eureka Server,然后启动eclient-1和eclient-2。证明一个Eureka Server不会影响服务的注册和发现。 停止eureka-1 ``` docker-compose stop eureka-1 ``` 重新启动eclient-1和eclient-2 ``` docker-compose start eclient-1 eclient-2 ``` # 演示zuul路由 启动另一套服务service-2和消费者feign-2。 |服务提供者|服务消费者| |---|---| |SERVICE-HI(eclient-1/eclient-2)|SERVICE-FEIGN-1(feign-1)| |SERVICE-2(service-2-1)|SERVICE-FEIGN-2(feign-2)| 讲解whoami接口的代码实现。 启动service-2-1 ``` docker-compose up -d service-2-1 ``` 启动feign-2 ``` docker-compose up -d feign-2 ``` 启动apigateway-1 ``` docker-compose up -d apigateway-1 ``` 讲解路由配置 分别在浏览器中访问: ``` http://localhost:8083/servicehi/whoami?name=lalalal ``` ``` http://localhost:8083/service2/whoami?name=lalalal ``` 查看结果,表示访问的是不同服务下的同名接口。 # 演示数据增删改查 访问查询接口 ``` http://localhost:8083/servicehi/findAllUser ``` ``` http://localhost:8083/servicehi/getuser?id=2 ``` 访问增加接口 ``` http://localhost:8083/servicehi/createUser?name=aaa&number=bbb ``` 访问修改接口 ``` http://localhost:8083/servicehi/updateUser?name=aaa&number=bbb&id= http://localhost:8083/servicehi/getuser?id=2 ``` 访问删除接口 ``` http://localhost:8083/servicehi/delUser?userId= ``` ``` http://localhost:8083/servicehi/findAllUser ``` # 演示zuul路由使用oauth2.0验证 关闭apigateway 启动oauth2server-1 ``` docker-compose up -d oauth2server-1 ``` 启动agw-oauth2 ``` docker-compose up -d agw-oauth2 ``` 先访问上一步的查询接口: ``` http://localhost:8085/servicehi/findAllUser ``` 由于没有令牌无法访问 访问如下地址,获取换取令牌的code ``` http://localhost:8084/oauth/authorize?client_id=client&scope=app&response_type=code&redirect_uri=http://www.baidu.com ``` 用reader/reader登录 获取换取令牌的code 用firefox调试工具,换取令牌 ``` http://client:secret@localhost:8084/oauth/token?grant_type=authorization_code&redirect_uri=http%3A%2F%2Fwww.baidu.com&code=[换取令牌的code] ``` 之前访问的接口加上令牌参数: ``` http://localhost:8085/servicehi/findAllUser?access_token=[令牌] ```