发表于: 2022-03-25 20:30:36
1 721
一,今天完成的事情
任务九
我选择的是任务三的Artstudio部分改成Spring Cloud展示,因为我要展示随机访问和OpenFeign的功能。其中包含 微服务A 使用 微服务B 的情况,更好展示Spring Cloud。
1,最终我的项目结构如下图:
2,我首先建立的是eureka,和之前一样,还是
server:
port: 8761
3,一步步拆解,先完成最底层
artstudioimageserviceone
只有一个,不展示负载均衡。这个组件供其它service调用,原来在项目三中我是没给controller的。
spring:
application:
name: ArtstudioImageServer
cloud:
loadbalancer:
ribbon:
enabled: false
datasource: #DB Configuration:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/art_room?useUnicode=true&characterEncoding=utf8
username: root
password: root
server:
port: 7011
eureka:
password: password
client:
serviceUrl:
defaultZone: http://user:${eureka.password}@localhost:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
metadataMap:
instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
mybatis:
# mapper文件
mapper-locations: mapper/*Dao.xml
# 实体类
type-aliases-package: com.nicole.imgs.entity
package com.nicole.studio.controller;
import com.nicole.studio.entity.ArtstudioImage;
import com.nicole.studio.service.ArtstudioImageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/artstudioimgs")
public class ArtstudioImageController {
private Logger logger = LoggerFactory.getLogger(ArtstudioImageController.class);
/**
* 服务对象
*/
@Autowired
private ArtstudioImageService artstudioImageService;
@RequestMapping (value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public List<ArtstudioImage> queryByArtstudioId(@PathVariable(name = "id") long id) {
List<ArtstudioImage> artstudioImages = this.artstudioImageService.queryByArtstudioId(id);
if ( null != artstudioImages ) {
return artstudioImages;
}
return null;
}
}
package com.nicole.studio;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author Nicole
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ArtstudioImageApplication {
public static void main(String[] args) {
SpringApplication.run(ArtstudioImageApplication.class, args);
}
}
运行eureka和artstudioimageserviceone能找到服务
成功通过设置的7011接口访问
4,artstudioserviceone和artstudioservicetwo。这两个准备做负载均衡。
有
去调用这篇日志中的artstudioimageserviceone。
package com.nicole.studio.client;
import com.nicole.studio.entity.ArtstudioImage;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@FeignClient("ArtstudioImageServer")
public interface ArtstudioImageClient {
@RequestMapping(method = RequestMethod.GET, value = "/artstudioimgs/{id}")
List<ArtstudioImage> queryByArtstudioId(@PathVariable(name = "id") long id);
}
ArtstudioImageServer
一定要大小写正确,一字不差
所以需要改变的地方有,原来是:
现在变成:
import com.nicole.studio.client.ArtstudioImageClient;
和
@Autowired
private ArtstudioImageClient artstudioImageClient;
启动类中
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ArtstudioApplication {
public static void main(String[] args) {
SpringApplication.run(ArtstudioApplication.class, args);
}
}
注意加上
@EnableFeignClients
eureka能成功访问到1台
端口7111成功
server:
port: 7111
端口7112eureka也正常注册,直接从7112访问也成功
server:
port: 7112
5,客户端
OpenFeign
package com.nicole.artstudio.controller;
import com.nicole.artstudio.client.ArtstudioServerClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/client")
public class ClientController {
@Autowired
private ArtstudioServerClient artstudioServerClient;
@RequestMapping (value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> selectOne(@PathVariable(name = "id") long id) {
return artstudioServerClient.selectOne(id);
}
}
server:
port: 7211
还没做负载均衡的时候,能够成功探测到client
从7211端口的client能正常访问。轮询方式
停止client。给client的controller加入
@RequestMapping("/random/{id}")
随机负载均衡访问的方式
package com.nicole.artstudio.controller;
import com.nicole.artstudio.client.ArtstudioServerClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@RestController
@RequestMapping("/client")
public class ClientController {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientController.class);
@Autowired
private ArtstudioServerClient artstudioServerClient;
@Autowired
RestTemplate template;
/**
* RoundRobin access with OpenFeign
*/
@RequestMapping (value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> selectOne(@PathVariable(name = "id") long id) {
return artstudioServerClient.selectOne(id);
}
/**
* Random access with load balancer of RestTemplate
*/
@RequestMapping("/random/{id}")
@ResponseBody
public String random(@PathVariable(name = "id") long id) {
String url = "http://ArtstudioServer/artstudio/"+id;
String callmeResponse = template.getForObject(url, String.class);
LOGGER.info("Response: {}", callmeResponse);
return callmeResponse;
}
}
多次点击都成功
如果为了在日志记录每次访问的接口,在artstudioservice层的controller加上
@Autowired
Environment environment;
变成
if ( null != artstudio ) {
return Restful.set(200, "show artstudio successfully "
+environment.getProperty("local.server.port"), artstudio);
}
logger打印
LOGGER.info("Response: {}", callmeResponse);
是保持50%情况下的随机访问,成功
7,WEB部分还是通过Nginx配置两台随机访问;并且两台WEB可以随机访问两台Service(其中之一)。是任务七中已经完成过的功能。
二,今天问题
如果用Spring Cloud LoadBalancer的 RestTemplate的用法,这里的
template.getForObject(url, String.class);
和OpenFeign没有关系。但是OpenFeign是微服务的必要功能,能够提供接口访问方式。
三,今天的收获
至少有两台service,web能随机访问其中任意一台。
项目可以加入其它Spring Cloud组件,展示其它组件的集成用法。目前首选Spring Cloud Gateway。
四,明天的计划
任务九深度思考
评论