Spring Cloud微服务学习系列9 URI和URL的区别及其关系

URI和URL的区别及其关系

解释1

统一资源标志符URI就是在某一规则下能把一个资源独一无二地标识出来。
拿人做例子,假设这个世界上所有人的名字都不能重复,那么名字就是URI的一个实例,通过名字这个字符串就可以标识出唯一的一个人。
现实当中名字当然是会重复的,所以身份证号才是URI,通过身份证号能让我们能且仅能确定一个人。
那统一资源定位符URL是什么呢。也拿人做例子然后跟HTTP的URL做类比,就可以有:
动物住址协议://地球/中国/浙江省/杭州市/西湖区/某大学/14号宿舍楼/525号寝/张三.人
可以看到,这个字符串同样标识出了唯一的一个人,起到了URI的作用,所以URL是URI的子集。URL是以描述人的位置来唯一确定一个人的。
在上文我们用身份证号也可以唯一确定一个人。对于这个在杭州的张三,我们也可以用:
身份证号:123456789
来标识他。
所以不论是用定位的方式还是用编号的方式,我们都可以唯一确定一个人,都是URl的一种实现,而URL就是用定位的方式实现的URI。
回到Web上,假设所有的Html文档都有唯一的编号,记作html:xxxxx,xxxxx是一串数字,即Html文档的身份证号码,这个能唯一标识一个Html文档,那么这个号码就是一个URI。
而URL则通过描述是哪个主机上哪个路径上的文件来唯一确定一个资源,也就是定位的方式来实现的URI。
对于现在网址我更倾向于叫它URL,毕竟它提供了资源的位置信息,如果有一天网址通过号码来标识变成了http://741236985.html,那感觉叫成URI更为合适,不过这样子的话还得想办法找到这个资源咯…

解释2

URI 在于I(Identifier)是统一资源标示符,可以唯一标识一个资源。
URL在于Locater,一般来说(URL)统一资源定位符,可以提供找到该资源的路径,比如http://www.zhihu.com/question/21950864,但URL又是URI,因为它可以标识一个资源,所以URL又是URI的子集。
举个是个URI但不是URL的例子:urn:isbn:0-486-27557-4,这个是一本书的isbn,可以唯一标识这本书,更确切说这个是URN。
总的来说,locators are also identifiers, so every URL is also a URI, but there are URIs which are not URLs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建一个新的Spring Boot项目 首先,我们需要在IDEA中创建一个新的Spring Boot项目。在IDEA的欢迎页面中选择“Create New Project”,然后选择“Spring Initializr”。 在Spring Initializr页面中,我们需要填写一些项目的基本信息,包括项目名称、描述、包名、Java版本、Spring Boot版本等。在这里,我们选择使用JDK 8,Spring Boot 2.2.2版本。 2. 添加Spring Cloud依赖 在创建完项目后,我们需要添加Spring Cloud的依赖。在pom.xml文件中添加以下依赖: ``` <dependencies> <!-- Spring Cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Others --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> </dependencies> ``` 这些依赖包括了Spring Cloud Eureka、Spring Cloud Config、Spring Cloud OpenFeign、Spring Cloud Zipkin等。 3. 配置Eureka注册中心 在application.yml文件中添加以下配置: ``` spring: application: name: service-demo cloud: config: uri: http://localhost:8888 profile: dev zipkin: base-url: http://localhost:9411 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 这里我们使用了Eureka作为服务注册中心,将服务的注册地址配置为http://localhost:8761/eureka/。 4. 创建服务接口 我们创建一个简单的服务接口,用于测试Spring Cloud的服务调用功能。 ``` public interface HelloService { @GetMapping("/hello") String hello(@RequestParam("name") String name); } ``` 5. 创建服务实现 创建服务实现类并使用@FeignClient注解标记为Feign客户端。 ``` @Service @FeignClient("service-provider") public class HelloServiceImpl implements HelloService { @Override public String hello(String name) { return "Hello " + name + "!"; } } ``` 6. 启用服务发现 在应用主类上添加@EnableDiscoveryClient注解以启用服务发现。 ``` @SpringBootApplication @EnableDiscoveryClient public class ServiceConsumerApplication { public static void main(String[] args) { SpringApplication.run(ServiceConsumerApplication.class, args); } } ``` 7. 测试服务调用 在测试类中注入HelloService并进行调用测试。 ``` @RunWith(SpringRunner.class) @SpringBootTest public class HelloServiceTest { @Autowired private HelloService helloService; @Test public void testHello() { String result = helloService.hello("world"); System.out.println(result); Assert.assertEquals("Hello world!", result); } } ``` 8. 启动服务并注册到Eureka 将服务启动后,可以在Eureka的管理页面中查看到该服务的注册信息。 至此,一个简单的Spring Cloud微服务项目就已经搭建完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值